package main

import (
	"fmt"
	"sync"
	"time"
)

var wg sync.WaitGroup

func p(c chan struct{}, num int) {
	defer wg.Done()
	defer func() {
		<-c
	}()
	fmt.Println(num)
	time.Sleep(time.Duration(num) * time.Second)
}

func main() {

	//s := make([]int, 100)
	c := make(chan struct{}, 5)

	for i := 0; i < 100; i++ {
		wg.Add(1)
		c <- struct{}{}
		go p(c, i)
	}
	wg.Wait()
}
